<template>
  <div class="main-content" style="display: flex; width: 80%;">
    <div style="margin: 0 auto; min-height: 1000px; flex: 4">
      <!--课程类型、课程名称-->
      <div style="text-align: center">
        <span style="font-size: 20px; font-weight: 550; color: #333333;">{{ courseData.name }}</span>
      </div>
      <!--   课程保密区域   -->
      <div style="margin-top: 10px">
        <div v-if="courseData.price === 0 || flag">
          <div v-if="courseData.type === 'VIDEO'">
            <video :src="videoList[playEpisodes]" controls style="width: 100%; height: auto"></video>
          </div>
          <el-button v-if="courseData.type === 'VIDEO'" @click="nextVideo" style="float: right">下一集</el-button>
          <div style="margin-top: 10px" v-if="courseData.file && courseData.file !== ''">资料链接：
            <a :href="courseData.file" target="_blank">{{
              courseData.file
            }}</a>
          </div>
        </div>
        <div v-else>
          <span style="color: red; margin-right: 20px">该课程属于付费课程，购买后可解锁</span>
          <el-button type="warning" size="mini" @click="buy">购买课程</el-button>
        </div>
      </div>
      <!--课程价格、课程折扣、课程发布时间-->
      <div style=" margin-top: 15px">
        <span style="color: red" v-if="courseData.price > 0">￥ {{ courseData.price }} 元</span>
        <span style="color: red" v-else>公开课</span>
        <span style="color: #509a4d; margin-left: 20px" v-if="courseData.discount < 1">{{
            courseData.discount * 10
          }} 折</span>
        <span style="color: #666666; margin-left: 50px">发布时间：{{ courseData.time }}</span>
      </div>
      <!--   课程介绍区域   -->
      <div style="margin-top: 20px">
        <div style="font-size: 18px">课程介绍</div>
        <div style="margin-top: 10px" v-html="courseData.content" class="w-e-text w-e-text-container"></div>
      </div>

      <div style="margin-top: 50px; font-size: 18px">欢迎发表您宝贵的意见</div>
      <div style="margin-top: 20px">
        <el-input type="textarea" :rows="5" v-model="content"></el-input>
      </div>
      <div style="margin-top: 10px; text-align: right">
        <el-button type="primary" @click="submit(content, 0)">提交</el-button>
      </div>
      <div style="margin-top: 30px; margin-bottom: 500px">
        <el-row v-for="item in commentData" style="margin-bottom: 30px">
          <el-col :span="4">
            <div style="display: flex; align-items: center;">
              <img :src="item.userAvatar" alt="" style="width: 50px; height: 50px; border-radius: 50%">
              <div style="flex: 1; margin-left: 10px">{{ item.userName }}</div>
            </div>
          </el-col>
          <el-col :span="20">
            <div style="height: 50px; line-height: 50px">
              <el-row>
                <el-col :span="18" style="white-space: nowrap; overflow: hidden; text-overflow: ellipsis;">
                  {{ item.content }}
                </el-col>
                <el-col :span="6" style="text-align: right">{{ item.time }}</el-col>
              </el-row>
            </div>
            <div v-for="child in item.children" style="margin-bottom: 5px">
              <el-row>
                <el-col :span="5">
                  <div style="display: flex; align-items: center;">
                    <img :src="child.userAvatar" alt="" style="width: 50px; height: 50px; border-radius: 50%">
                    <div style="flex: 1; margin-left: 10px">{{ child.userName }} 回复：</div>
                  </div>
                </el-col>
                <el-col :span="13"
                        style="height: 50px; line-height: 50px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;">
                  {{ child.content }}
                </el-col>
                <el-col :span="6" style="height: 50px; line-height: 50px; text-align: right">{{ child.time }}</el-col>
              </el-row>
            </div>
            <div style="margin-top: 20px">
              <el-input style="width: 400px" v-model="item.tmp"></el-input>
              <el-button type="primary" style="margin-left: 5px" @click="submit(item.tmp, item.id)">回复</el-button>
            </div>
          </el-col>
        </el-row>
      </div>
    </div>
    <div v-if="courseData.type === 'VIDEO'" style="flex: 1;  margin-left:10px; margin-top: 40px; background-color: #f1f2f3; max-height: 300px; min-height: 200px; border-radius: 2%">
      <!--   课程选集   -->
      <div style="text-align: center">
        <span style="font-size: 15px; font-weight: 550; color: #333333;">课程选集</span>
      </div>
      <div style="margin:10px ">
          <el-row :gutter="0">
                <el-col :span="5" v-for="(item, index ) in videoList" :key="index">
                  <el-button @click="navToVideo(index)">{{ index + 1 }}</el-button>
                </el-col>
          </el-row>
      </div>
    </div>
  </div>
</template>
<script>
import E from 'wangeditor'

export default {
  data() {
    let courseId = this.$route.query.id
    return {
      // 尝试从浏览器的本地存储（localStorage）中获取键名为'xm-user'的项
      //localStorage是一个简单的键值存储系统，允许你存储数据在用户的浏览器中，即使用户关闭浏览器，数据也不会丢失。
      // getItem方法用于检索本地存储中与指定键关联的值。
      user: JSON.parse(localStorage.getItem('xm-user') || '{}'),
      courseId: courseId,
      courseData: {},
      flag: false,
      content: null,
      commentData: [],
      videoList: [],//存放视频的url地址
      playEpisodes: 0,
    }
  },
  mounted() {
    this.loadCourse();
    this.checkCourse();
    this.loadComment();
  },
  methods: {
    // 发出get请求，请求course详细信息
    loadCourse() {
      this.$request.get('/course/selectById/' + this.courseId).then(res => {
        if (res.code === '200') {
          this.courseData = res.data
          if (this.courseData.type === 'VIDEO') {
            // 将字符串分割,获取到每个视频的url
            this.videoList = this.courseData.video.split(",");
          }
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    // 修改课程访问权限
    checkCourse() {
      this.$request.get('/orders/selectAll', {
        params: {
          userId: this.user.id,
          courseId: this.courseId
        }
      }).then(res => {
        if (res.code === '200') {
          if (res.data.length > 0) {
            this.flag = true
          }
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    // 购买课程，传入购买的课程，购买的用户
    buy() {
      let data = {
        courseId: this.courseId,
        userId: this.user.id
      }
      this.$request.post('/orders/add', data).then(res => {
        if (res.code === '200') {
          this.$message.success('购买成功，已解锁课程')
          this.loadCourse()
          this.checkCourse()
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    submit(content, parentId) {
      let data = {
        userId: this.user.id,
        courseId: this.courseId,
        content: content,
        parentId: parentId,
      }
      this.$request.post('/comment/add', data).then(res => {
        if (res.code === '200') {
          this.$message.success('评论成功')
          // 清空评论区输入框内容
          this.content = null
          this.loadComment()
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    loadComment() {
      this.$request.get('/comment/selectAll', {
        params: {
          courseId: this.courseId
        }
      }).then(res => {
        if (res.code === '200') {
          this.commentData = res.data
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    //播放下一集
    nextVideo() {
      if (this.playEpisodes < this.videoList.length - 1) {
        this.playEpisodes++;
      } else {
        this.$message.error("视频集数达到最大");
      }
    },
    navToVideo(episode) {
      this.playEpisodes = episode
    }
  }
}
</script>